import json
import os

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

# 设置 Edge WebDriver 的路径
edge_driver_path = "C:/xiaoyh/edge_driver/msedgedriver.exe"

# 设置 Edge 浏览器的选项
options = Options()
options.add_argument('--headless')  # 无头模式
options.add_argument('--disable-gpu')  # 禁用GPU加速
options.add_argument('--no-sandbox')  # 解决权限问题

# 启动 Microsoft Edge 浏览器
driver = webdriver.Edge(service=Service(edge_driver_path), options=options)

# 定义动态等待方法
def wait_for_elements(context, by, value, timeout=10):
    """等待指定元素加载完成"""
    return WebDriverWait(context, timeout).until(
        EC.presence_of_all_elements_located((by, value))
    )

# 定义点击 '下一页' 按钮的方法
def get_next_page_link(driver):
    """获取 '查看更多' 按钮的链接"""
    try:
        next_page = driver.find_element(By.CLASS_NAME, "amore")
        next_page_link = next_page.get_attribute("href")
        print(f"下一页链接: {next_page_link}")
        return next_page_link
    except Exception as e:
        print("无法找到 '查看更多' 按钮:", e)
        return None

try:
    # 打开网页
    url = "https://www.gushici.net/zuozhe/"
    driver.get(url)

    sanitized_mod_name = "作者"

    # 设置抓取目标数量
    target_count = 1020  # 例如，抓取100条数据
    data_list = []  # 使用列表存储抓取的数据
    index = 1

    while len(data_list) < target_count:
        # 获取文章元素
        author_elements = wait_for_elements(driver, By.XPATH, "//div[@class='left']//div[@class='shici-pic']",
                                            timeout=10)
        print(f"当前页面抓取到 {len(author_elements)} 位作者信息")

        for author in author_elements:
            if len(data_list) >= target_count:
                break  # 达到目标数量，退出循环
            try:
                author_box = author.find_element(By.CLASS_NAME, "shici-pic-box")
                author_name = author_box.find_elements(By.TAG_NAME, "p")[0].text.strip()
                # 获取作者描述（第二个 p 标签），排除 a 标签内容
                author_desc_element = author_box.find_elements(By.TAG_NAME, "p")[1]
                a_text = author_desc_element.find_element(By.TAG_NAME, "a").text.strip()
                author_desc = author_desc_element.text.strip().replace(a_text, "")

                # 添加到列表中
                data_list.append({"index": index, "name": author_name, "desc": author_desc})
                index += 1
            except Exception as author_error:
                print(f"解析单篇作者信息失败: {author_error}")

        print(f"已抓取 {len(data_list)} 条数据")

        if len(data_list) >= target_count:
            print("已达到目标数量，停止抓取。")
            break

        # 获取下一页链接
        next_page_link = get_next_page_link(driver)
        if next_page_link:
            driver.get(next_page_link)
            # 使用显式等待，确保新页面加载完成
            try:
                WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.XPATH, "//div[@class='left']//div[@class='shici-pic']"))
                )
            except Exception as e:
                print("新页面加载超时:", e)
                break
        else:
            print("没有找到下一页链接，停止抓取。")
            break

    # 写入数据到 JSON 文件
    if data_list:
        output_dir = "output/selenium/zuozhe"
        os.makedirs(output_dir, exist_ok=True)
        filename = os.path.join(output_dir, f"{sanitized_mod_name}.json")
        with open(filename, "w", encoding="utf-8") as file:
            json.dump(data_list, file, ensure_ascii=False, indent=4)
        print(f"模块 {sanitized_mod_name} 抓取成功，共 {len(data_list)} 条数据")
    else:
        print(f"模块 {sanitized_mod_name} 没有可抓取的文章数据，跳过写入。")

    print(f"{sanitized_mod_name} 模块抓取完成\n{'-' * 60}")

except Exception as e:
    print("发生错误:", e)
finally:
    driver.quit()
    print("程序已退出")
